import Vue from 'vue'
import VueRouter from 'vue-router'
import { formatRoutes } from '@/utils/router-utils'
// import { constantRouters } from './router-local'
import { constantRouters } from './router-async'

// 处理重复点击，重复push，控制台报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

/**
 * 初始化路由实例
 * 
 * @param {Boolean} isAsync 是否异步路由 
 * @returns { Object } router
 */
function createRouter (isAsync) {
  formatRoutes(constantRouters)
  return new VueRouter({
    mode: "history",
    routes: constantRouters,
    scrollBehavior: () => ({ y: 0 })
  })
}

export default createRouter(false)
